package com.pig4cloud.pigx.system.websocket.okey;


import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pigx.common.customize.redis.constants.CommonRedisConstants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ParseData {

	public static String HEARTBEAT = "pong";

	public static String source_name = "bitmex";
	public static void parse(String msg){
		try{
			if(msg.equals(HEARTBEAT)) {
				return;
			}
			JSONObject json = JSONObject.parseObject(msg);
			if(json!= null){
				if(json.getJSONObject("arg") == null) {
					return;
				}
				if(json.getJSONArray("data") == null) {
					return;
				}

				String channel = json.getJSONObject("arg").getString("channel");
				String symbol =  json.getJSONObject("arg").getString("instId").replace("-","").replace("SWAP","");
				JSONObject jsondata =  json.getJSONArray("data").getJSONObject(0);
				switch (channel){
					case "index-tickers" :
						String k_index = CommonRedisConstants.OKX + CommonRedisConstants.INDEX_PRICE + symbol ;
						String v_index =jsondata.getString("idxPx") + "_" + jsondata.getString("ts");
						WsManager.getInstance().fetchManager.setdata(k_index,v_index);
						break;
					case "mark-price":
						String k_mark = CommonRedisConstants.OKX + CommonRedisConstants.MARK_PRICE +  symbol;
						String v_mark =jsondata.getString("markPx") + "_" + jsondata.getString("ts");
						WsManager.getInstance().fetchManager.setdata(k_mark,v_mark);
						break;
					case "tickers":
						String k_last = CommonRedisConstants.OKX + CommonRedisConstants.LAST_PRICE +  symbol;
						String v_last =jsondata.getString("last") + "_" + jsondata.getString("ts");
						WsManager.getInstance().fetchManager.setdata(k_last,v_last);
						break;
					default:
						break;
				}
			}

		}catch (Exception ex){
			log.error("parse okx data error  "+ex.getMessage());
		}
	}

}
